import "LotroLCD.LotroLCD.UI";

LotroLCDConfigWindow = class(LotroLCDWindow);
function LotroLCDConfigWindow:Constructor(parent, settings, plugin)
	Turbine.UI.Lotro.Window.Constructor(self);
	
	-- Keep track of parent and base plugin object
	self.parent = parent;
	self.plugin = plugin;
	
	-- Associate settings
	self.settings = settings;
	
	-- Set Look, feel, size  and position
	self:SetBackColorBlendMode(Turbine.UI.BlendMode.Normal);
	self:SetText("Settings");
	self:SetSize(300, 200);
	self:SetVisible(settings.visibility);
	self:SetOpacity(settings.opacity);
	self:SetZOrder(1);
	
	-- Manually center the settings window
	local displayWidth, displayHeight = Turbine.UI.Display.GetSize();
    local windowWidth, windowHeight = self:GetSize();  
    self:SetPosition((displayWidth - windowWidth) / 2, 100);
	
	-- Create a tooltip object
	self.tooltips = LotroLCDTooltipWindow(self.settings);
	
	-- Create a popup edit object to edit strings
	self.editPopup = LotroLCDEditPopupWindow(self, self.settings, plugin);
	
	local genericSettingsLabel = Turbine.UI.Label();
	genericSettingsLabel:SetPosition(30, 40);
	genericSettingsLabel:SetSize(200, 25);
	genericSettingsLabel:SetFontStyle(Turbine.UI.FontStyle.Outline);
	genericSettingsLabel:SetFont(Turbine.UI.Lotro.Font.Verdana18);
	genericSettingsLabel:SetTextAlignment(Turbine.UI.ContentAlignment.TopLeft);
	genericSettingsLabel:SetText("Generic Settings");
	genericSettingsLabel:SetParent(self);
	
	local opacityLabel = Turbine.UI.Label();
	opacityLabel:SetPosition(50, 70);
	opacityLabel:SetSize(100, 25);
	opacityLabel:SetFontStyle(Turbine.UI.FontStyle.Outline);
	opacityLabel:SetFont(Turbine.UI.Lotro.Font.Verdana14);
	opacityLabel:SetTextAlignment(Turbine.UI.ContentAlignment.TopLeft);
	opacityLabel:SetText("Opacity");
	opacityLabel:SetParent(self);
	
	-- Opacity slider
	local opacitySlider =  Turbine.UI.Lotro.ScrollBar(); 
	opacitySlider:SetPosition(150, 75);
	opacitySlider:SetBackColor( Turbine.UI.Color(0.1, 0.1, 0.1));
	opacitySlider:SetSize(100, 10);
	opacitySlider:SetParent(self);
	opacitySlider:SetValue(settings.opacity * 100);
	
	opacitySlider.ValueChanged = function( sender, args )
		local opacity = opacitySlider:GetValue();
		parent.settings.opacity = opacity / 100;
		self.settings.opacity = parent.settings.opacity;
		self:SetOpacity(self.settings.opacity);
		parent:SetOpacity(self.settings.opacity);
		parent.placeholder:SetOpacity(self.settings.opacity);
		parent:SaveSettings();
	end
	
	local renameButton = Turbine.UI.Lotro.Button();
	renameButton:SetParent( self );
	renameButton:SetPosition(50, 100);
	renameButton:SetSize(70, 20);
	renameButton:SetText("Rename");
	renameButton.MouseClick = function( sender, args )
		if self.editPopup:IsVisible() then
			self.editPopup:Deactivate()
		else
			local value = parent:GetText();
			self.editPopup:Activate(settings, value, parent)
		end
	end
	
	local cloneButton = Turbine.UI.Lotro.Button();
	cloneButton:SetParent( self );
	cloneButton:SetPosition( 130, 100);
	cloneButton:SetSize(60, 20);
	cloneButton:SetText("Clone");
	cloneButton.MouseClick = function( sender, args )
		--Deep copy settings to new table, add it to settings, instanciate, move and save
		table.insert(plugin.settings.windows, deepcopy(parent.settings))
		table.insert(plugin.windows, deepcopy(getmetatable(parent)).__index(plugin, plugin.settings.windows[#plugin.settings.windows], plugin));
		plugin.windows[#plugin.windows]:SetPosition((displayWidth - windowWidth) / 2, 200);
		plugin:SaveSettings();
	end
	
	local deleteButton = Turbine.UI.Lotro.Button();
	deleteButton:SetParent( self );
	deleteButton:SetPosition( 200, 100);
	deleteButton:SetSize(70, 20);
	deleteButton:SetText("Delete");
	deleteButton.MouseClick = function( sender, args )
		-- Delete window from plugin window list
		-- Delete windows from settings and save
		for i in ipairs(plugin.windows) do
			if plugin.windows[i] == parent then
				table.remove(plugin.windows,i);
				table.remove(plugin.settings.windows,i);
				break
			end
		end
		plugin:SaveSettings();
		--Override default hide behaviour
		parent.VisibleChanged = function( sender, args)	end
		parent:SetVisible(false);
		parent = nil;
		self:SetVisible(false);
		self = nil;
	end
	
	local dataElementsLabel = Turbine.UI.Label();
	dataElementsLabel:SetPosition(30, 130);
	dataElementsLabel:SetSize(200, 25);
	dataElementsLabel:SetFontStyle(Turbine.UI.FontStyle.Outline);
	dataElementsLabel:SetFont(Turbine.UI.Lotro.Font.Verdana18);
	dataElementsLabel:SetTextAlignment(Turbine.UI.ContentAlignment.TopLeft);
	dataElementsLabel:SetText("Data Elements");
	dataElementsLabel:SetParent(self);
	
	-- Listbox to automatically layout multiple widgets as a group
	self.listbox = Turbine.UI.ListBox();
	self.listbox:SetParent(self);
	self.listbox:SetPosition(50, 160);
	self.listbox:SetSize(120, 100);
	
	-- Add label/button to config window for each supported data element
	self.sortedElements = dictToSortedArrayOfDict(parent.settings.elements);
	self:RefreshListbox(self.sortedElements, self.listbox)
	
	-- Add Version Label
	self:ConstructVersionLabel(self);
end

-- Add a push button to a listbox, resizing it and it's parent window
function LotroLCDConfigWindow:RefreshListbox(elements, listbox)
	for i,v in ipairs(elements) do
		local width, height = listbox:GetSize();
		-- Create new button for each element
		local listItem = Turbine.UI.Lotro.Button();
		
		-- Set Look, feel, size  and position
		listItem:SetSize(100, 20);
		listItem:SetFontStyle(Turbine.UI.FontStyle.Outline);
		listItem:SetFont(Turbine.UI.Lotro.Font.Verdana14);
		listItem:SetTextAlignment(Turbine.UI.ContentAlignment.MiddleCenter);
		listItem:SetText(v[1]);
		
		if v[2] > 0 then -- If element is visible
			listItem:SetBackColor(Turbine.UI.Color(0, 0.2, 0));
		else
			listItem:SetBackColor(Turbine.UI.Color(0.2, 0, 0));
		end
		
		-- Add to window listbox and scale master window
		listbox:AddItem(listItem);
		self:SetHeight(height + 110);
		listbox:SetHeight(height + 20);
		
		--Tooltips
		listItem.MouseHover = function(sender, args)
			local x, y = listItem:GetPosition();
			local boxX, boxY = self:PointToScreen(listbox:GetPosition());
			local tooltipSettings = {};
			tooltipSettings.x = boxX + x + 110;
			tooltipSettings.y = boxY + y;
			local tooltipText = self.plugin.settings.elementDescriptions[listItem:GetText()]
			if tooltipText then
				self.tooltips:Activate(tooltipSettings, self.plugin.settings.elementDescriptions[tostring(listItem:GetText())]);
			end
		end
		listItem.MouseLeave = function(sender, args)
			self.tooltips:Deactivate();
		end
		
		listItem.MouseClick = function(sender, args)
			if elements[i][2] > 0 then
				elements[i][2] = 0;
			else
				elements[i][2] = listbox:IndexOfItem(listItem);
			end
			
			-- Assign new position value to dictionary based on array
			reProcessDictByParentArray(elements);
			
			-- Convert config settings to plane dictionary and save
			self.parent.settings.elements = arrayOfDictToDict(elements);
			self.settings.elements = arrayOfDictToDict(elements);
			self:SaveSettings();
			
			-- Create a resorted array
			elements = dictToSortedArrayOfDict(self.settings.elements);
			
			-- Create a resorted array of enabled elements
			self.parent.enabledElements = dictOfKeyPositionToSortedArray(self.settings.elements);

			-- Rebuild the listbox in the config window
			self:RebuildConfigListBox(elements,listbox);
			
			-- Update Parent Window, valueListbox
			if self.parent.valueListbox then
				self.parent:RefreshListbox(self.plugin:GetElementValues(self.parent.enabledElements), self.parent.valueListbox);	
			end
			
			-- Update Parent Window, keyListbox
			if self.parent.keyListbox then
				self.parent:RefreshListbox(self.parent.enabledElements, self.parent.keyListbox);
				-- Rebuild key listbox if present
				self:RebuildConfigListBox(self.parent.enabledElements, self.parent.keyListbox);
			end
		end
	end
end

-- Rebuilds list boxes from new a new array of dictionaries or sorted array
function LotroLCDConfigWindow:RebuildConfigListBox(elements,listbox)
	for i,v in ipairs(elements) do
		local key;
		local value;
		local currentItem = listbox:GetItem(i);
		--Handle input as array of dictionaries or sorted array
		if type(v) == "table" then
			value = v[1];
			key = v[2];
			
			-- If item is in an array of dictionaries and is visible
			if key > 0 then
				currentItem:SetBackColor( Turbine.UI.Color( 0, 0.2, 0 ) );
			else
				currentItem:SetBackColor( Turbine.UI.Color( 0.2, 0, 0 ) );
			end
		else
			value = v;
			key = i;
		end
		currentItem:SetText(value);
	end
end
